package util.grepkey;


/**
 * Created by huashao on 16/8/8.
 */

public class GrabEncryptStrategy  implements IEncryptStrategy{

    public String secure(String message, String extra) {
        try {
            String encryptKey = "D820LKJFU720OT82"+message;
            String en = encrypt(encryptKey,extra);
            return SHAUtil.shaHex(en).toLowerCase();
        }catch (Exception e){

        }
        return "";
    }

    private String encrypt(String data,String extra){
        String encryptKey = "0123456789abcdef";
        byte b[] = data.getBytes();
        byte bytes[] = extra.getBytes();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            sb.append(encryptKey.charAt(b[i] >>> 4 & 0x0F));
            if (bytes.length>=b.length) {
                sb.append(encryptKey.charAt(~(bytes[i] >>> 4) & 0x0F)) ;
            }else{
                sb.append(encryptKey.charAt(~(bytes[i%bytes.length] >>> 4)& 0x0F)) ;
            }
            sb.append(encryptKey.charAt(b[i] & 0x0F));
            String str = sb.toString();
            sb.append(String.valueOf(str.hashCode()).substring(0,1));
            if (bytes.length>=b.length) {
                sb.append(encryptKey.charAt(bytes[i]& 0x0F)) ;
            }else{
                sb.append(encryptKey.charAt(bytes[i%bytes.length] & 0x0F)) ;
            }
        }
        return sb.toString();
    }

    @Override
    public String secure(String message, String extra,String encryptData) {

        return "";
    }
}
